
/**
 * ajax请求
 * @param config 请求参数配置
 * @param successfn 请求成功执行函数
 */
function ajaxRequest(config,successfn){
    this.config={
        url: config.url,
        type: config.type|| "post",
        async:config.async|| "true",
        cache:config.cache|| "false",
        data:config.data|| {},
        dataType:config.dataType|| "json",
        contentType:config.contentType|| "application/x-www-form-urlencoded",
    };
    var headers={
        "Authorization": "Bearer "+localStorage.getItem("Authorization"),
    };//请求头
    $.ajax({
        url: this.config.url,
        headers:headers,
        type: this.config.type,
        async:this.config.async,
        cache:this.config.cache,
        data: this.config.data,
        dataType: this.config.dataType,
        contentType:this.config.dataType,
        success: function (data) {
            if (typeof (successfn) === "function") {
                successfn(data);
            }
        },
        error: function (res) {
            showAlert('错误信息', "系统错误，请联系系统管理员", "error");
        }
    });
}

/**
 * 表单提交请求
 * @param config 请求参数配置
 * @param successfn 请求成功执行函数
 */
function formSubmit(form,url,successfn){
    $('#'+form).form("submit", {
        url: url,
        onSubmit: function (param) {
            //进行表单字段验证，当全部字段都有效时返回 true
            var isValid = $('#' +form).form('validate');
            if (!isValid) {
                // hide progress bar while the form is invalid
                $.messager.progress('close');
            }
            return isValid;
        },
        success: function (data) {
            var e = eval('(' + data + ')');
            if (e.code == 0) {
                if (typeof (successfn) === "function") {
                    successfn(e);
                }
            } else {
                showAlert('信息', e.result, "error");
            }
        }
    });
}